#!/bin/bash

data_dir=/sys/kernel/debug/gcov
temp_dir=$(mktemp -d)
echo "[INFO] Start collecting the profiles."
find ${data_dir} -type d -exec mkdir -p ${temp_dir}/\{\} \;
find ${data_dir} -name '*.gcda' -exec sh -c 'cat < $0 > '${temp_dir}'/$0' {} \;
find ${data_dir} -name '*.gcno' -exec sh -c 'cp -d $0 '${temp_dir}'/$0' {} \;

echo "[INFO] Start post-processing the profiles."
find ${temp_dir} -name '*.gcda' > list.txt
/usr/bin/g++ GcovSummaryAddTool.cpp -o calcsum
./calcsum list.txt
#rm -rf list.txt

profile_dir=gcovdata
rm -rf ${profile_dir}
mkdir ${profile_dir}
for file in $(find ${temp_dir} -name '*.gcda');do
hash_path=$(echo ${file//\//\#})
name=$(echo ${hash_path#*gcov})
mv $file ${profile_dir}/$name
done
rm -rf ${temp_dir}
